const fs = require('fs')
const path = require('path')
const userSQL = require('../db/userSQL')
const goodsSQL = require('../db/goodsSQL')
const {db,pool}=require('../db/db')

const getList = async (id, c, p) => {
  let result = await db(goodsSQL.queryAll, [])
  let user = await db(userSQL.queryAll, [])
  let data=JSON.stringify(result)
  const data1=JSON.parse(data)
  let str=''
  data1.forEach(item=>{
    let userId=item.user_id
    let username=user.find(item=>item.id===userId).username
    let val=JSON.stringify(item)
    str+=val.slice(0,-1)+',"username":"'+username+'"},'
  })
  let info1='['+str.slice(0,-1)+']'
  let info=JSON.parse(info1)
  return info
}

const add = async (id, gId) => {
  let result = await db(userSQL.queryAll, [])
  let data=JSON.stringify(result)
  const users = JSON.parse(data)
  const cart = users.find(item => item.id === id)

  let result2 = await db(goodsSQL.queryAll, [])
  let data2=JSON.stringify(result2)
  const item = JSON.parse(data2).find(item => item.goods_id === gId)
  item.is_collect = !item.is_collect//修改goods中的is_collect
  if (!item) return 0
  let val
  if(cart.cart==null){//判断是否有数据
    val='['+JSON.stringify(item)+']'
  }else{
    var str1 = cart.cart.substr(1);//删除首字节
    val='['+str1.substring(0,str1.length-1)+','+JSON.stringify(item)+']'
  }
  pool.query(userSQL.updateCart,[val,cart.username],(err,result)=>{undefined
    if(err) throw err;
    console.log(result);
  });

  return 'ok'
}

const select = async (goods_id) => {
  let result = await db(goodsSQL.queryAll, [])
  let data=JSON.stringify(result)
  const goods_sel = JSON.parse(data).find(item => item.goods_id === goods_id)

  goods_sel.admin_select= !goods_sel.admin_select
  console.log(goods_sel.admin_select)
  pool.query(goodsSQL.updateAdminGoods,[Number(goods_sel.admin_select),goods_id],(err,result)=>{undefined
    if(err) throw err;
    console.log(result);
  });

  return 'ok'
}


const remove = async (goods_id) => {
    pool.query(goodsSQL.deleteGoods,[goods_id],(err,result)=>{undefined
     if(err) throw err;
     console.log(result);
   });
  return 'ok'
}


const selectAll = async (id, type) => {
  let result = await db(goodsSQL.queryAll, [])
  let data=JSON.stringify(result)
  const goods_sel = JSON.parse(data)
  goods_sel.forEach(item => {
    item.admin_select =type ? true :false
    pool.query(goodsSQL.updateAdminGoods,[Number(item.admin_select),item.goods_id],(err,result)=>{undefined
      if(err) throw err;
      console.log(result);
    });
  })
  return 'ok'
}

const removeSelect = async id => {
  let result = await db(goodsSQL.queryAll, [])
  let data=JSON.stringify(result)
  const goods_sel = JSON.parse(data)
  goods_sel.forEach(item => {
    if(Number(item.admin_select)===1){
      pool.query(goodsSQL.deleteGoods,[item.goods_id],(err,result)=>{undefined
       if(err) throw err;
       console.log(result);
      });
    }
  })
  return 'ok'
}

module.exports = {
  getList,
  add,
  select,
  remove,
  selectAll,
  removeSelect
}
